<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图 - 运动结束</title>
    <style>
        *{margin: 0;padding: 0;}
        ul, ol, li {
            list-style: none;
        }
        .banner {
            width: 600px;
            height: 400px;
            border: 10px solid #333;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
        }
        .banner > ul {
            width: 500%;
            overflow: hidden;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
        }
        .banner > ul > li {
            height: 100%;
            width: 600px;
            float: left;
            line-height: 400px;
            font-size: 100px;
            text-align: center;
            color: #fff;
        }
        .banner > ol {
            width: 175px;
            height: 25px;
            background-color: rgb(0, 0, 0, .5);
            border-radius: 15px;
            position: absolute;
            left: 50%;
            bottom: 30px;
            margin-left: -93px;
            padding-top: 5px;
            padding-right: 15px;
        }
        .banner > ol > li {
            width: 20px;
            height: 20px;
            background-color: #fff;
            border-radius: 50%;
            float: left;
            margin-left: 15px;
            cursor: pointer;
        }
        .banner > ol > li.active {
            background-color: orange;
        }
        .banner > div {
            width: 100%;
            height: 60px;
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -30px;
        }
        .banner > div > p {
            float: left;
            width: 40px;
            height: 100%;
            background-color: rgb(0, 0, 0, .5);
            line-height: 60px;
            text-align: center;
            font-size: 30px;
            color: #fff;
            cursor: pointer;
        }
        .banner > div > p:last-child {
            float: right;
        }
    </style>
</head>
<body>
    <div class="banner">
        <ul class="img_box">
            <li style="background-color: red;">1</li>
            <li style="background-color: skyblue;">2</li>
            <li style="background-color: violet;">3</li>
            <li style="background-color: black;">4</li>
            <li style="background-color: palegreen;">5</li>
        </ul>
        <ol>
            <!-- <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li> -->
        </ol>
        <div>
            <p><</p>
            <p>></p>
        </div>
    </div>

    <script src="./utils.js"></script>
    <script>
        /**
         * 布局
         * 焦点
         * 复制
         * 自动
         * 结束
        */
        const imgBox = document.querySelector('.banner > ul')
        const pointBox = document.querySelector('.banner > ol')
        const banner = document.querySelector('.banner')
        const banner_width = banner.clientWidth
        let timer = 0       // 
        let index = 1       // 第几张

        setPoint()
        function setPoint() {
            var num = imgBox.children.length
            const frg = document.createDocumentFragment()
            for (let i = 0; i < num; i++) {
                const li = document.createElement('li')
                if (i === 0) li.classList.add('active')
                frg.appendChild(li)
            }
            pointBox.appendChild(frg)

            pointBox.style.width = num * 35 + 'px'
            pointBox.style.marginLeft = -pointBox.offsetWidth / 2 + 'px'
        }

        copyEle()
        function copyEle() {
            const first = imgBox.firstElementChild.cloneNode(true)
            const last = imgBox.lastElementChild.cloneNode(true)
            // console.log(first, last)
            imgBox.appendChild(first)
            imgBox.insertBefore(last, imgBox.firstChild)
            imgBox.style.width = imgBox.children.length * 100 + '%'
            imgBox.style.left = -banner_width + 'px'
        }

        autoPlay()
        function autoPlay() {
            timer = setInterval(() => {
                index++
                move(imgBox, { left: -index * banner_width }, moveEnd)
            }, 2000)
        }

        function moveEnd() {
            if (index === imgBox.children.length - 1) {
                index = 1   
                imgBox.style.left = -banner_width + 'px'
            }
            for (let i = 0; i < pointBox.children.length; i++) {
                pointBox.children[i].className = ''
            }
            pointBox.children[index - 1].className = 'active'
        }

    </script>
</body>

</html>